package com.example.yaoalarm;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.TextView;

import java.util.Calendar;

public class ring extends AppCompatActivity implements SensorEventListener {

    private TextView nowtime;
    private TextView nums;
    private Intent intent;
    private Vibrator vibrator;
    private int i = 30;
    //定义sensor管理器
    private SensorManager mSensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ring);

        nowtime= this.findViewById(R.id.tmgp);
        nums = this.findViewById(R.id.tbugg);
        nums.setText(""+i);

        Calendar ca=Calendar.getInstance();
        int a = ca.get(Calendar.HOUR_OF_DAY);
        int b = ca.get(Calendar.MINUTE);
        if(a < 10 && b < 10)
            nowtime.setText("0"+a+":0"+b);
        else if(a < 10)
            nowtime.setText("0"+a+":"+b);
        else if(b < 10)
            nowtime.setText(a+":0"+b);
        else
            nowtime.setText(a+":"+b);

        // 启动服务播放背景音乐
        intent = new Intent(ring.this, MyIntentService.class);
        String action = MyIntentService.ACTION_MUSIC;
        // 设置action
        intent.setAction(action);
        startService(intent);

        vibrator = (Vibrator)this.getSystemService(this.VIBRATOR_SERVICE);
        long[] patter = {0, 1000, 0, 1000};
        vibrator.vibrate(patter, 1);

        //获取传感器管理服务
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }


    @Override
    protected void onResume(){
        super.onResume();
        //加速度传感器
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                //还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等，
                //根据不同应用，需要的反应速率不同，具体根据实际情况设定
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onStop(){
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    protected void onPause() {
        mSensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
        //当传感器精度改变时回调该方法，Do nothing.
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        int sensorType = event.sensor.getType();
        //values[0]:X轴，values[1]：Y轴，values[2]：Z轴
        float[] values = event.values;
        if(sensorType == Sensor.TYPE_ACCELEROMETER){
            /*因为一般正常情况下，任意轴数值最大就在9.8~10之间，只有在你突然摇动手机
             *的时候，瞬时加速度才会突然增大或减少。
             *所以，经过实际测试，只需监听任一轴的加速度大于14的时候，改变你需要的设置就OK了~~~
             */
            if((Math.abs(values[0])>14||Math.abs(values[1])>14||Math.abs(values[2])>14)){
                //事件
                i--;
                changei();
                if(i == 0)
                    finish();
            }
        }
    }

    public void changei(){
        nums.setText(""+i);
    }
    @Override
    public void finish() {
        // TODO Auto-generated method stub
        if(i == 0){
            vibrator.cancel();
            onDestroy();
            super.finish();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (intent != null){
            // 对于intentService，这一步可能是多余的
            stopService(intent);
        }
    }
}